package com.eco.fanliapp.admin.controller.web;

import com.eco.fanliapp.admin.annotation.SysLogAnnotation;
import com.eco.fanliapp.common.response.ServerResponse;
import com.eco.fanliapp.entity.AppVersion;
import com.eco.fanliapp.service.IAppVersionService;
import com.eco.fanliapp.service.RedisTemplateService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/web/version")
public class AppVersionController {
    @Autowired
    private IAppVersionService appVersionService ;
    @Autowired
    private RedisTemplateService redisTemplateService;

    @SysLogAnnotation(value = "app版本新增",userType = 2)
    @PostMapping("/insertAppVersion")
    public ServerResponse<List> insertAppVersion(@RequestBody AppVersion appVersion){
        return appVersionService.insert(appVersion);
    }

    @SysLogAnnotation(value = "app版本修改",userType = 2)
    @PostMapping("/updateAppVersion")
    public ServerResponse<List> updateAppVersion(@RequestBody AppVersion appVersion){
        Integer id = appVersion.getId();
        appVersion.setUpdateTime(System.currentTimeMillis());
        if(StringUtils.isEmpty(id)){
            return appVersionService.insert(appVersion);
        }
        String redisKeyStr = "app_version_" + appVersion.getSystem();
        redisTemplateService.set(redisKeyStr, appVersion, 60 * 5);
        return appVersionService.updateByPrimaryKey(appVersion);
    }

    /**
     * 版本列表
     */
    @RequestMapping(method = RequestMethod.GET,value = "/selectAppVersionList")
    public ServerResponse<PageInfo> selectAppVersionList(@RequestParam(defaultValue = "1") Integer currentPage ,
                                                         @RequestParam(defaultValue = "5") Integer pageSize){
        return  appVersionService.selectAppVersionList(currentPage,pageSize);
    }

    @SysLogAnnotation(value = "app版本删除",userType = 2)
    @RequestMapping(method = RequestMethod.GET,value = "/delete")
    public ServerResponse delete(Integer id){
        int i = appVersionService.deleteAppVersionById(id);
        if(i >0){
            return ServerResponse.createBySuccess();
        }
        return ServerResponse.createByError();
    }

}
